package community.hospital.service.application.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.common.BaseRemove;
import community.hospital.common.CustomerException;
import community.hospital.convert.CommonConvert;
import community.hospital.convert.PageConvert;
import community.hospital.convert.RoleConvert;
import community.hospital.entity.Menu;
import community.hospital.entity.Role;
import community.hospital.entity.RoleMenuRelation;
import community.hospital.entity.request.ListRoleRequest;
import community.hospital.entity.request.RoleRequest;
import community.hospital.entity.response.RoleResponse;
import community.hospital.service.application.MenuApplicationService;
import community.hospital.service.application.RoleApplicationService;
import community.hospital.service.domain.MenuService;
import community.hospital.service.domain.RoleMenuRelationService;
import community.hospital.service.domain.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static community.hospital.enums.BusinessEnum.ROLE_REPEAT;

/**
 * @Author：ydj
 * @Date：2023/1/26 14:43
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleApplicationServiceImpl implements RoleApplicationService {
    @Resource
    private RoleService roleService;
    @Resource
    private RoleMenuRelationService roleMenuRelationService;
    @Resource
    private MenuApplicationService menuApplicationService;
    @Resource
    private MenuService menuService;
    @Override
    public Boolean addRole(RoleRequest request) {
        //判断角色是否重复用添加
        int count = roleService.countByRoleNameAndRoleCode(request.getRoleCode(), request.getRoleName());
        if (count > 0) {
            throw new CustomerException(ROLE_REPEAT);
        }
        Role role = RoleConvert.INSTANCE.convertToRole(request);
        roleService.save(role);
        List<RoleMenuRelation> menuRelationList = getMenuRelationListByMenuIds(role.getId(),request.getMenuId());
        return roleMenuRelationService.saveBatch(menuRelationList);
    }



    @Override
    public Boolean removeRole(BaseRemove request) {
        List<Long> roleIds = request.getIds();
        //删除角色
        roleService.removeByIds(roleIds);
        //删除关系
        return roleMenuRelationService.removeByRoleIds(roleIds);
    }

    @Override

    public Boolean updateRole(RoleRequest request) {
        int count = roleService.countByRoleNameAndRoleCode(request.getRoleCode(), request.getRoleName());
        if (count > 0) {
            throw new CustomerException(ROLE_REPEAT);
        }
        Role role = RoleConvert.INSTANCE.convertToRole(request);
        List<Long> menuIdList = request.getMenuId();
        //针对关系表 先删在存，针对role表进行更新
        roleMenuRelationService.removeByRoleId(role.getId());
        List<RoleMenuRelation> roleMenuRelations = getMenuRelationListByMenuIds(role.getId(), menuIdList);
        roleMenuRelationService.saveBatch(roleMenuRelations);
        return roleService.updateById(role);

    }

    @Override
    public Page<RoleResponse> listRole(ListRoleRequest request) {
        Page<Role> rolePage = roleService.listRole(request);
        Page<RoleResponse> roleResponsePage = PageConvert.convert(rolePage, RoleConvert.INSTANCE::convertToResp);
        roleResponsePage.getRecords().forEach(roleResponse->{
            //根据角色id查询权限
            List<Menu> menuList = menuApplicationService.listMenuByRoleId(roleResponse.getId());
            roleResponse.setMenuList(menuList);
        });
        return roleResponsePage;
    }

    @Override
    public List<RoleResponse> listAllRole() {
        List<Role> roleList = roleService.listAllRole();
        List<RoleResponse> responseList = CommonConvert.convert(roleList, RoleConvert.INSTANCE::convertToResp);
        return responseList;
    }

    private List<RoleMenuRelation> getMenuRelationListByMenuIds(Long roleId,List<Long> menuIds) {
        return menuIds.stream().map(menuId -> {
            RoleMenuRelation relation = new RoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            return relation;
        }).collect(Collectors.toList());
    }


}
